package com.sunnada.nginx.controller;

import com.alibaba.fastjson.JSON;
import com.sunnada.nginx.bean.nginx.http.upstream.Upstream;
import com.sunnada.nginx.exception.CustonException;
import com.sunnada.nginx.service.UpstreamService;
import com.sunnada.nginx.service.UpstreamServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "上游配置", description = "上游配置")
@RestController
@RequestMapping("upstreams")
public class UpstreamController {

    @Autowired
    UpstreamService upstreamService;

    @ApiOperation(value="获取上游配置列表", notes="获取上游配置列表",httpMethod = "GET")
    @GetMapping
    @ResponseBody
    public Object list() throws CustonException {return upstreamService.list();}

    @ApiOperation(value="获取上游配置", notes="获取上游配置",httpMethod = "GET")
    @ApiImplicitParam(name = "name", value = "上游配置名称", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{name}")
    @ResponseBody
    public Object one(@PathVariable String name) throws CustonException { return upstreamService.one(name); }


    @ApiOperation(value="新增上游配置", notes="新增上游配置",httpMethod = "POST")
    @PostMapping
    @ResponseBody
    public String post(Upstream upstream) throws CustonException{
//        Upstream upstream = JSON.parseObject(json, Upstream.class);
        return upstreamService.post(upstream)?"success":"fail";
    }

    @ApiOperation(value="修改上游配置", notes="修改上游配置",httpMethod = "PUT")
    @PutMapping
    @ResponseBody
    public String put(Upstream upstream) throws CustonException{
//        Upstream upstream = JSON.parseObject(json, Upstream.class);
        return upstreamService.put(upstream)?"success":"fail";
    }


    @ApiOperation(value="删除上游配置", notes="删除上游配置",httpMethod = "DELETE")
    @ApiImplicitParam(name = "name", value = "上游配置名称", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{name}")
    @ResponseBody
    public String delete(@PathVariable String name) throws CustonException{ return upstreamService.delete(name)?"success":"fail"; }
}
